package com.base.controller.utils.CZB;

import com.base.helper.VosHelper;
import com.base.po.param.InfoCustomers;
import com.base.po.param.VosModelList;
import com.base.po.voip.Subscribers;
import com.base.po.voip.Users;
import com.base.service.SubscribersService;
import com.base.utils.GsonUtil;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.math.RoundingMode;

@Log4j
@Component
public class XMTUtils {
    @Autowired
    SubscribersService subscribersService;

    @PostConstruct
    public void init() {
        xmtUtils = this;
    }

    public static XMTUtils xmtUtils;

    /**
     * @Date: 2020/6/16 9:54
     * @Description: 厦门通先扣购物金再扣话费
     */
    public static void koufei(Users agent, Subscribers user, BigDecimal money) {
        try {
            if (user.getShopBalance() >= money.intValue()) {
                BigDecimal moneykou = money.setScale(0, RoundingMode.HALF_EVEN);
                System.out.println("加油扣费:" + moneykou);
                Integer i = moneykou.intValue();
                Integer integer = user.getShopBalance();
                System.out.println("扣费:" + (integer + i));
                user.setShopBalance(integer + i);
                xmtUtils.subscribersService.updateByPrimaryKeySelective(user);
                log.warn("[厦门通] 购物金扣除成功" + user.getMobile());
            } else {
                String results1 = VosHelper.GetVosAccount(
                        "{accounts:['" + agent.getAgentprefix() + user.getMobile() + "']}");
                VosModelList v21 = GsonUtil.getObject(results1, VosModelList.class);
                if (v21 != null && v21.retCode == 0) {
                    InfoCustomers info2 = v21.infoCustomers[0];
//                    if (info2.getMoney().compareTo(money.abs()) > 0) {
                        String payinfo = VosHelper.PayVosAccount("{\"ownerName\":\"" + agent.getAgentprefix() + user.getMobile()
                                + "\",\"ownerType\":\"2\",\"money\":\"" + money + "\",\"memo\":\"" + user.getUsername()
                                + "，用户油站购买油的扣费" + money + "元\"}");
                        log.warn("[厦门通] 话费扣除成功" + user.getMobile());
//                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    }
